package com.yfp.device.service.impl;

import com.yfp.device.domain.MachineDownLog;
import com.yfp.device.domain.MachineLoginLog;
import com.yfp.device.domain.MachineOpLog;
import com.yfp.device.domain.R;
import com.yfp.device.domain.vo.MachineDownLogVo;
import com.yfp.device.domain.vo.MachineLoginLogVo;
import com.yfp.device.domain.vo.MachineOpLogVo;
import com.yfp.device.mapper.MachineDownLogMapper;
import com.yfp.device.mapper.MachineLoginLogMapper;
import com.yfp.device.mapper.MachineOpLogMapper;
import com.yfp.device.service.MachineLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 机器日志服务
 */
@Service
@Slf4j
public class MachineLogServiceImpl implements MachineLogService {

    private final MachineLoginLogMapper machineLoginLogMapper;

    private final MachineDownLogMapper machineDownLogMapper;

    private final MachineOpLogMapper machineOpLogMapper;

    public MachineLogServiceImpl(MachineLoginLogMapper machineLoginLogMapper, MachineDownLogMapper machineDownLogMapper, MachineOpLogMapper machineOpLogMapper) {
        this.machineLoginLogMapper = machineLoginLogMapper;
        this.machineDownLogMapper = machineDownLogMapper;
        this.machineOpLogMapper = machineOpLogMapper;
    }

    /**
     * 上传崩溃日志
     *
     * @param machineSn        机器编号
     * @param machineDownLogVo 崩溃日志
     * @return 响应体
     */
    @Override
    public R<?> uploadMachineDownLog(String machineSn, MachineDownLogVo machineDownLogVo) {
        try {
            MachineDownLog machineDownLog = new MachineDownLog();
            machineDownLog.setMachineId(machineSn);
            machineDownLog.setClearFlag(0);
            machineDownLog.setCreateTime(new Date());
            machineDownLog.setDownTime(machineDownLogVo.getDownTime());
            machineDownLog.setDownReason(machineDownLogVo.getDownReason());
            machineDownLogMapper.insertSelective(machineDownLog);
            log.info("上传崩溃日志成功");
            return R.ok();
        } catch (Exception e) {
            log.error("上传崩溃日志失败", e);
            return R.fail("上传崩溃日志失败");
        }
    }

    /**
     * 上传登录日志
     *
     * @param machineSn         机器编号
     * @param machineLoginLogVo 登录日志
     * @return 响应体
     */
    @Override
    public R<?> uploadMachineLoginLog(String machineSn, MachineLoginLogVo machineLoginLogVo) {
        try {
            MachineLoginLog machineLoginLog = new MachineLoginLog();
            machineLoginLog.setMachineId(machineSn);
            machineLoginLog.setClearFlag(0);
            machineLoginLog.setCreateTime(new Date());
            machineLoginLog.setLoginStatus(machineLoginLogVo.getLoginStatus());
            machineLoginLog.setLoginTime(machineLoginLogVo.getLoginTime());
            machineLoginLog.setUserId(machineLoginLogVo.getUserId());
            machineLoginLog.setUserName(machineLoginLogVo.getUserName());
            machineLoginLogMapper.insertSelective(machineLoginLog);
            log.info("上传登录日志成功");
            return R.ok();
        } catch (Exception e) {
            log.error("上传登录日志失败", e);
            return R.fail("上传登录日志失败");
        }
    }

    /**
     * 上传操作日志
     *
     * @param machineSn      机器编号
     * @param machineOpLogVo 操作日志
     * @return 响应体
     */
    @Override
    public R<?> uploadMachineOpLog(String machineSn, MachineOpLogVo machineOpLogVo) {
        try {
            MachineOpLog machineOpLog = new MachineOpLog();
            machineOpLog.setMachineId(machineSn);
            machineOpLog.setClearFlag(0);
            machineOpLog.setCreateTime(new Date());
            machineOpLog.setUserId(machineOpLogVo.getUserId());
            machineOpLog.setUserName(machineOpLogVo.getUserName());
            machineOpLog.setOpTime(machineOpLogVo.getOpTime());
            machineOpLog.setOperation(machineOpLogVo.getOperation());
            machineOpLogMapper.insertSelective(machineOpLog);
            log.info("上传操作日志成功");
            return R.ok();
        } catch (Exception e) {
            log.error("上传操作日志失败", e);
            return R.fail("上传操作日志失败");
        }
    }
}
